package ServerTCP;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerTCP {

    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(8888);
            System.out.println("启动服务器....");
            while(true){
                Socket s = ss.accept();
                System.out.println("客户端:"+s.getInetAddress().getLocalHost()+"已连接到服务器");

                BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                //读取客户端发送来的消息，代表开始测量延迟和吞吐量
                String mess = br.readLine();
                while(mess.equals("begin")){
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                    bw.write("receive\n");
                    bw.flush();
                    //建立三个TCP连接用来并发传输
                    for(int i=0;i<3;++i)
                    {
                        Socket socket= ss.accept();
                        System.out.println("开始启动新线程");
                        ThroughputTCP Throughput=new ThroughputTCP(socket);
                        Throughput.start();
                    }
                    mess=br.readLine();
                    /*if(mess.equals("over")||mess==null){
                        break;
                    }*/
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

